package com.example.yichuangyun.mvvm.ui.fragment.view;

import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;

import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.model.call.CallRecordTransModel;
import com.example.yichuangyun.mvvm.base.BaseViewModel;
import com.example.yichuangyun.mvvm.di.retrofit.BaseModel;
import com.example.yichuangyun.mvvm.di.retrofit.BaseNetCallback;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

public class CallDetailsTextViewModel extends BaseViewModel {

    public MutableLiveData<CallRecordTransModel> transTextSuccess = new MutableLiveData<>();
    public MutableLiveData<String> requestAgain = new MutableLiveData<>();

    /**
     * 录音文件转文字
     * type -1 第一次拉取   0 沟通内容   1 智能需求分析   2 沟通内容重新生成（已经废弃 不重复生成）
     * requestId "DB"/"ALI"
     */
    public void getTranscription(String recordId, String url, int type, String requestId) {
        Map<String, Object> map = new HashMap<>();
        map.put("recordId", recordId);
        map.put("type", type);
        map.put("url", url);
        map.put("requestId", requestId);

        addSubscribe(mRetrofitHelper.getTranscription(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<CallRecordTransModel>() {
            @Override
            public void onSuccess(BaseModel<CallRecordTransModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (baseModel.getData() != null) {
                        transTextSuccess.setValue(baseModel.getData());
                    }
                } else {
                    /*{
                        "code": 0,
                        "msg": "该录音正在转换中，请等待处理完成！",
                        "data": null
                       }*/
                    if (baseModel.getMsg().equals("该录音正在转换中，请等待处理完成！")) {
                        requestAgain.setValue(baseModel.getMsg());
                    } else {
                        mErrorData.setValue(baseModel.getMsg());
                    }
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                if (!TextUtils.isEmpty(message) && message.equals("timeout")) {
                    requestAgain.setValue(message);
                } else {
                    mErrorData.setValue(message);
                }
            }
        }));
    }
}
